package com.meiyu.live.ui.main;

import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.meiyu.live.BaseFragment;
import com.meiyu.live.R;
import com.meiyu.live.comm.Constant;
import com.meiyu.live.net.param.BaseBodyParams;
import com.meiyu.live.net.response.IndexAllResponse;
import com.meiyu.live.ui.main.widget.ColumnItemView;

/**
 * A simple {@link Fragment} subclass.
 */
public class ColumnAllFragment extends BaseFragment {

    private SwipeRefreshLayout refreshLayout;
    private ListView listView;
    private RecommendListAdapter adapter;

    private IndexAllResponse indexAllResponse;

    public ColumnAllFragment() {
    }

    public static ColumnAllFragment newInstance() {
        ColumnAllFragment fragment = new ColumnAllFragment();
        return fragment;
    }

    @Override
    public int getContextView() {
        return R.layout.fragment_columnall;
    }

    @Override
    protected void init() {
        refreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getData(true);
            }
        });
        listView = (ListView) findViewById(R.id.listview);
        adapter = new RecommendListAdapter();
        listView.setAdapter(adapter);

        getData(true);
    }

    private void getData(final boolean reload) {
        //全部栏目接口
        request(new BaseBodyParams(Constant.UrlParams.INDEXALL), IndexAllResponse.class, new Response.Listener<IndexAllResponse>() {
            @Override
            public void onResponse(IndexAllResponse indexAllResponse) {
                refreshLayout.setRefreshing(false);
                if (indexAllResponse != null) {
                    ColumnAllFragment.this.indexAllResponse = indexAllResponse;
                    adapter.notifyDataSetChanged();
                }
            }
        });
    }

    @Override
    public void onErrorResponse(VolleyError volleyError) {
        refreshLayout.setRefreshing(false);
        super.onErrorResponse(volleyError);
    }

    class RecommendListAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return indexAllResponse != null && indexAllResponse.getHash() != null ? indexAllResponse.getHash().size() : 0;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ColumnItemView columnItemView = null;
            if (convertView == null) {
                columnItemView = new ColumnItemView(getActivity());
                convertView = columnItemView;
            } else {
                columnItemView = (ColumnItemView) convertView;
            }
            columnItemView.setData(indexAllResponse.getHash().get(position));
            return convertView;
        }
    }
}
